﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class ItemNoDA : IItemNoDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string ITEM_NO_ID = "@ITEM_NO_ID";
        private string ITEM_NO_UOM = "@ITEM_NO_UOM";
        private string SQL_CHECK_ITEM_NO_ID_UNIQUE = " SELECT COUNT(1) FROM @_@ITEM_NO WHERE ITEM_NO_ID = @ITEM_NO_ID ";
        private string SQL_CHECK_ITEM_NO_ID_UNIQUE_IS_ACTIVE = " SELECT COUNT(1) FROM @_@ITEM_NO WHERE ITEM_NO_ID = @ITEM_NO_ID AND IS_ACTIVE='Y' ";
        private string SQL_DELETE_ITEM_NO = " DELETE FROM @_@ITEM_NO WHERE ITEM_NO_ID = @ITEM_NO_ID ";
        private string SQL_INSERT_ITEM_NO = " INSERT INTO @_@ITEM_NO ( WH_ID, ITEM_NO_ID, DESCR, ITEM_NO_UOM, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @ITEM_NO_ID, @DESCR, @ITEM_NO_UOM, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_ITEM_NO = " SELECT  ROWID, WH_ID, ITEM_NO_ID, DESCR, ITEM_NO_UOM, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@ITEM_NO ";
        private string SQL_UPDATE_ITEM_NO = " UPDATE @_@ITEM_NO SET WH_ID = @WH_ID, DESCR = @DESCR, ITEM_NO_UOM = @ITEM_NO_UOM, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE ITEM_NO_ID = @ITEM_NO_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckItemNoIDUnique(string itemNoID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemNoID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_ITEM_NO_ID_UNIQUE_IS_ACTIVE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckItemNoIDUnique(DataBase dataBase, DbTransaction tran, string itemNoID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemNoID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_ITEM_NO_ID_UNIQUE_IS_ACTIVE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckItemNoIDUniqueInsert(string itemNoID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemNoID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_ITEM_NO_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckItemNoIDUniqueInsert(DataBase dataBase, DbTransaction tran, string itemNoID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemNoID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_ITEM_NO_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteItemNo(List<string> itemNoID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < itemNoID.Count; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemNoID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ITEM_NO.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteItemNo(DataBase dataBase, DbTransaction tran, List<string> itemNoID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < itemNoID.Count; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemNoID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_ITEM_NO.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllItemNo(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ITEM_NO.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllItemNo(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ITEM_NO.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public ItemNoInfo GetItemNoByID(string itemnoID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ITEM_NO.Replace("@_@", whLoginID) + " WHERE ITEM_NO_ID = @ITEM_NO_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemnoID) };
            ItemNoInfo itemNoInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    itemNoInfo = new ItemNoInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ITEM_NO_ID"].ToString(), reader["DESCR"].ToString(), reader["ITEM_NO_UOM"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return itemNoInfo;
        }

        public ItemNoInfo GetItemNoByID(DataBase dataBase, DbTransaction tran, string itemnoID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ITEM_NO.Replace("@_@", whLoginID) + " WHERE ITEM_NO_ID = @ITEM_NO_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemnoID) };
            ItemNoInfo itemNoInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                itemNoInfo = new ItemNoInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["ITEM_NO_ID"].ToString(), reader["DESCR"].ToString(), reader["ITEM_NO_UOM"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return itemNoInfo;
        }

        public DataSet GetItemNoByQueryList(List<string> sqlWhere, ItemNoQueryEntity itemnoQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ITEM_NO.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!itemnoQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, itemnoQuery.CurrentPage, itemnoQuery.PageSize, itemnoQuery.SortField, itemnoQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetItemNoByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, ItemNoQueryEntity itemnoQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ITEM_NO.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, itemnoQuery.CurrentPage, itemnoQuery.PageSize, itemnoQuery.SortField, itemnoQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public string GetItemNoDescrByID(string itemnoID, string whLoginID)
        {
            string sql = "SELECT DESCR FROM @_@ITEM_NO WHERE ITEM_NO_ID = @ITEM_NO_ID  ";
            sql = sql.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemnoID) };
            string ItemNoDescr = "";
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    ItemNoDescr = reader["DESCR"].ToString();
                }
                if (!reader.IsClosed)
                {
                    reader.Close();
                }
            }
            return ItemNoDescr;
        }

        public string GetItemNoDescrByID(DataBase dataBase, DbTransaction tran, string itemnoID, string whLoginID)
        {
            string sql = "SELECT DESCR FROM @_@ITEM_NO WHERE ITEM_NO_ID = @ITEM_NO_ID  ";
            sql = sql.Replace("@_@", whLoginID);
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ITEM_NO_ID", itemnoID) };
            string ItemNoDescr = "";
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                ItemNoDescr = reader["DESCR"].ToString();
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return ItemNoDescr;
        }

        public int InsertItemNo(ItemNoInfo itemNoInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                itemNoInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ItemNo_Parameters(itemNoInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ITEM_NO.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertItemNo(DataBase dataBase, DbTransaction tran, ItemNoInfo itemNoInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                itemNoInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ItemNo_Parameters(itemNoInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ITEM_NO.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_ItemNo_Parameters(ItemNoInfo itemNoInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.ITEM_NO_ID, SqlDbType.VarChar, 50), new SqlParameter(this.DESCR, SqlDbType.VarChar, 200), new SqlParameter(this.ITEM_NO_UOM, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(itemNoInfo.WhID))
            {
                paramArray[0].Value = itemNoInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = itemNoInfo.ItemNoID;
            if (!string.IsNullOrEmpty(itemNoInfo.Descr))
            {
                paramArray[2].Value = itemNoInfo.Descr;
            }
            else
            {
                paramArray[2].Value = DBNull.Value;
            }
            paramArray[3].Value = itemNoInfo.ItemNoUom;
            paramArray[4].Value = itemNoInfo.IsActive;
            paramArray[5].Value = itemNoInfo.CreatedBy;
            paramArray[6].Value = itemNoInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateItemNo(ItemNoInfo itemNoInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                itemNoInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ItemNo_Parameters(itemNoInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ITEM_NO.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateItemNo(DataBase dataBase, DbTransaction tran, ItemNoInfo itemNoInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                itemNoInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_ItemNo_Parameters(itemNoInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ITEM_NO.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

